package com.xof.phonecall.telecom

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telecom.Call
import com.xof.phonecall.databinding.ActivityOngoingCallBinding

//        1   = Call.STATE_DIALING
//        2   = Call.STATE_RINGING
//        3   = Call.STATE_HOLDING
//        4   = Call.STATE_ACTIVE
//        7   = Call.STATE_DISCONNECTED
//        8   = Call.STATE_SELECT_PHONE_ACCOUNT
//        9   = Call.STATE_CONNECTING
//        10  = Call.STATE_DISCONNECTING
//        11  = Call.STATE_PULLING_CALL
class OngoingCallActivity : AppCompatActivity() {

    lateinit var mActivityBinding: ActivityOngoingCallBinding

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mActivityBinding = ActivityOngoingCallBinding.inflate(layoutInflater)
        setContentView(mActivityBinding.root)

        MyCallManager.registerCallback(object : Call.Callback() {
            override fun onStateChanged(call: Call?, state: Int) {
                /*
                    Call states:
                     1   = Call.STATE_DIALING
                     2   = Call.STATE_RINGING
                     3   = Call.STATE_HOLDING
                     4   = Call.STATE_ACTIVE
                     7   = Call.STATE_DISCONNECTED
                     8   = Call.STATE_SELECT_PHONE_ACCOUNT
                     9   = Call.STATE_CONNECTING
                     10  = Call.STATE_DISCONNECTING
                     11  = Call.STATE_PULLING_CALL
                */
                println("=====onStateChanged:$state")
                super.onStateChanged(call, state)

                var status = mActivityBinding.textCallStatus.text.toString()
                if (status.isBlank()) {
                    status = "状态：${getStatusDes(state)}"
                } else {
                    status += "\n"
                    status += "状态：${getStatusDes(state)}"
                }
                mActivityBinding.textCallStatus.text = status

                if (7 == state) {
                    mActivityBinding.textCallNow.text = "拨打过程已结束: ${MyCallManager.mPhoneNumber}"
                    //清空正在拨打标志
                    MyCallManager.mPhoneNumber = ""
                }
            }
        })

        mActivityBinding.textCallNow.text = "正在拨打电话: ${MyCallManager.mPhoneNumber}"
    }

    fun getStatusDes(state: Int): String {
        when (state) {
            1 -> {
                return "STATE_DIALING-外拔呼叫中..."
            }
            2 -> {
                return "STATE_RINGING-来电响铃中..."
            }
            3 -> {
                return "STATE_HOLDING-本机接听中..."
            }
            4 -> {
                return "STATE_ACTIVE-通话中..."
            }
            7 -> {
                return "STATE_DISCONNECTED-已经断开连接..."
            }
            8 -> {
                return "STATE_SELECT_PHONE_ACCOUNT-选择手机账号..."
            }
            9 -> {
                return "STATE_CONNECTING-连接中..."
            }
            10 -> {
                return "STATE_DISCONNECTING-正在断开连接..."
            }
            11 -> {
                return "STATE_PULLING_CALL-正在尝试建立连接..."
            }
        }
        return "未知"
    }
}